package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.bean.Result;
import com.bdqn.bean.user.User;
import com.bdqn.service.IUserService;
import com.bdqn.service.imp.UserServiceImp;
import com.bdqn.utli.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.HttpURLConnection;

/**
 * @author 忍
 * @title: UserController
 * @projectName Teaching
 * @description: TODO
 * @date 2019/4/279:23
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    HttpServletRequest request;
    /**
     * @description: 根据手机号获取用户
     * @param phone
     * @return com.bdqn.bean.user.User
     * @throws
     * @author Mr.Zhou
     * @date 2019/4/27 0:25
     */
    @GetMapping("/getphonelogincode/{phone}")
    public String getPhoneLoginCode(@PathVariable String phone){
        int phoneCheckNo = NoteSend.createCode();
        System.out.println("验证码:"+phoneCheckNo);
        HttpSession session = request.getSession();
        session.setAttribute("phoneCheckNo",phoneCheckNo);
        session.setAttribute("phoneNo",phone);
//        return NoteSend.commonRpc(phone,phoneCheckNo);w
        EmailCheck.sendeCode(phone,"",phoneCheckNo+"");
        return JSON.toJSONString(new Result("Message","OK"));
    }
    @GetMapping("/phonelogin/{code}&{phone}")
    public String getUserByPhone( @PathVariable Integer code,@PathVariable String phone,HttpServletResponse response){
        if (code!=null && phone!=null){
            HttpSession session = request.getSession();
            Integer code1 = (Integer) session.getAttribute("phoneCheckNo");
            String phone1 = (String)session.getAttribute("phoneNo");
            if (code1!=null && code.equals(code1) && phone1!=null && phone1.equals(phone)){
                User user = userService.selectUserByPhone(phone);
                if (user==null){
                    return JSON.toJSONString(new Result("fail","账号未注册"));
                }
//                response.setHeader("Authorization",JwtUtil.sign(user.getPhone(),user.getId()+""));
                return JSON.toJSONString(new Result("success", JwtUtil.sign(user.getId()+"")));
            }else {
                Result result = new Result("fail", "验证码输入不正确");
                String s = JSON.toJSONString(result);
                return JSON.toJSONString(result);
            }
        }
        return JSON.toJSONString(new Result("fail","请输入手机号"));
    }
    @GetMapping("/pwdlogin/{pwd}&{phone}")
    public String getUserByPwd(@PathVariable String phone,@PathVariable String pwd,HttpServletRequest request){
        if (pwd!=null && phone!=null){
            User user = userService.selectUserByPwd(phone,pwd);
            if (user==null){
                return JSON.toJSONString(new Result("fail","账号/密码错误"));
            }
            String token =  JwtUtil.sign(user.getId()+"");
            return JSON.toJSONString(new Result("success",token));
        }
        return JSON.toJSONString(new Result("fail","请输入手机号/密码"));
    }

    /**
     * @description:添加用户
     * @param user
     * @return void
     * @throws
     * @author Mr.Zhou
     * @date 2019/5/10 14:09
     */
    @PostMapping("/{code}")
    public String addUser(@RequestBody User user,@PathVariable Integer code ){
        try{
            if (code!=null && user!=null){
                HttpSession session = request.getSession();
                Integer code1 = (Integer) session.getAttribute("phoneCheckNo");
                if (code1!=null && code.equals(code1)){
//                    user.setPwd(Encrypt.string2MD5(user.getPwd()));
//                    userService.addUser(user);
                    return JSON.toJSONString("success");
                }else {
                    Result result = new Result("fail", "验证码输入不正确");
                    String s = JSON.toJSONString(result);
                    return JSON.toJSONString(result);
                }
            }
            return "fail";
        }catch (Exception e){
            e.printStackTrace();
            return JSON.toJSONString("fail");
        }
    }
    @GetMapping("/{phone}")
    public String selectUserByPhone(@PathVariable String phone){
        User user = userService.selectUserByPhone(phone);
        if (user==null){
            return JSON.toJSONString(new Result("fail","账号未注册"));
        }
        return JSON.toJSONString(new Result("success","账号已存在"));
    }
}
